<?php
/**
 * 获取修复结果
 */
include_once __DIR__ . '/../BaseShell.php';

//修复中批次
$batchs = Module_Repair_RepairBatch::getInstance()->getAllBySearch(['id','batchNo','repairType'],['where'=>["batchNo<>''","repairStatus = 3"]]);
foreach ($batchs as $batch){
    //现在用再芮整合渠道
    $use_pass = Lib_Repair_Config::getConfig('use_pass',0);
    if($use_pass == 'ZaiRui'){
        $repair_result = Lib_Repair_Implement_ZaiRui::getInstance()->getRepairResult(['batchNo'=>$batch['batchNo']]);
        if($repair_result['code'] != 200){//请求返回异常
            Func_Log::write('repair','zairui','get repair result -- error response '.'responsedata : '. json_encode($repair_result));
            break;
        }
        if(in_array($repair_result['data']['batchStatus'],[0,3])){//待修复或修复中
            Func_Log::write('repair','zairui','error: get repair result --Under repair  response '.'responsedata : '. json_encode($repair_result));
            break;
        }
        //再芮只返回成功修复的
        $repairData = $repair_result['data']['matchResult'];
        //idcard 做数组键值来判断是否成功
        $repairData = array_column($repairData,NULL,'idCard');
        //获取该批次下所有案件，循环提交处理修复成功活失败
        $records = Module_Repair_RepairRecord::getInstance()->getAllBySearch(['id','sha256idCard','repairName','idCard'],['where'=>["batchId={$batch['id']}"]]);
        foreach ($records as $record){
            $tasks_data = [
                'recordId' => $record['id'],
                'idCard' => $record['idCard'],
                'sha256idCard' => $record['sha256idCard'],
                'repairName' => $record['repairName'],
                'batchNo' => $batch['batchNo'],
                'repairType' => $batch['repairType'],
                'telXData' => []
            ];
            //判断成功数据里面是否有改记录，有则成功
            if(isset($repairData[$record['sha256idCard']])){

                $tasks_data['repairStatus'] = $repairData[$record['sha256idCard']]['status'] == 2 ? 3 : 4;
                $tasks_data['resultId'] = $repairData[$record['sha256idCard']]['repairRecordID'];
                $tasks_data['telXData'] = $repairData[$record['sha256idCard']]['telXData'];
                $tasks = [
                    'type' => 'Module_Task_Repair',
                    'method' => 'zrResult',
                    'data' =>$tasks_data
                ];
                Func_Log::write('repair',   'liantong','task '. json_encode($tasks));
                @Module_Queue_Base::addQueue($tasks,0,2);
            }elseif (Lib_Redis_String::get($record['sha256idCard'].'_'.$batch['repairType'])){//如果没有修复成功判断是否24小时内修复过，如果存在则是修复失败
                $tasks_data['repairStatus'] = 4;
                $tasks = [
                    'type' => 'Module_Task_Repair',
                    'method' => 'zrResult',
                    'data' =>$tasks_data
                ];
                Func_Log::write('repair',   'zairui','task '. json_encode($tasks));
                @Module_Queue_Base::addQueue($tasks,0,2);
            }
        }
        //添加更新批次案件状态队列
        $tasks = [
            'type' => 'Module_Task_Repair',
            'method' => 'updateBatch',
            'data' =>['batchId'=>$batch['id'],'repairType'=>$batch['repairType'],'batchNo'=>$batch['batchNo']]
        ];
        Func_Log::write('repair',   'zairui','task '. json_encode($tasks));
        @Module_Queue_Base::addQueue($tasks,0,2);
    }else{
        //移动查看修复状态
        if($batch['repairType'] == 1){
            $repair_result = Lib_Repair_Implement_YiDong::getInstance()->getRepairResult(['batchNo'=>$batch['batchNo']]);
            if(!$repair_result['success'] && empty($repair_result['data'])){
                Func_Log::write('repair','yidong','get repair result -- error response '.'responsedata : '. json_encode($repair_result));
                break;
            }
            if(in_array($repair_result['data']['batchStatus'],[1,2])){//待修复或修复中
                Func_Log::write('repair','yidong','error: get repair result --Under repair  response '.'responsedata : '. json_encode($repair_result));
                break;
            }
            //移动只返回成功修复的
            $repairData = $repair_result['data']['matchResult'];
            //idcard 做数组键值来判断是否成功
            $repairData = array_column($repairData,NULL,'idCard');
            //获取该批次下所有案件，循环提交处理修复成功活失败
            $records = Module_Repair_RepairRecord::getInstance()->getAllBySearch(['id','sha256idCard','repairName','idCard'],['where'=>["batchId={$batch['id']}"]]);
            foreach ($records as $record){
                $tasks_data = [
                    'recordId' => $record['id'],
                    'idCard' => $record['idCard'],
                    'sha256idCard' => $record['sha256idCard'],
                    'repairName' => $record['repairName'],
                    'batchNo' => $batch['batchNo'],
                    'phoneList' => []
                ];
                //判断成功数据里面是否有改记录，有则成功
                if(isset($repairData[$record['sha256idCard']])){
                    $tasks_data['repairStatus'] = 3;
                    $tasks_data['resultId'] = $repairData[$record['sha256idCard']]['resultId'];
                    $tasks_data['phoneList'] = $repairData[$record['sha256idCard']]['phoneList'];
                    $tasks = [
                        'type' => 'Module_Task_Repair',
                        'method' => 'ydResult',
                        'data' =>$tasks_data
                    ];
                    Func_Log::write('repair',   'yidong','task '. json_encode($tasks));
                    @Module_Queue_Base::addQueue($tasks,0,2);
                }elseif (Lib_Redis_String::get($val['sha256idCard'].'_'.$batch['repairType'])){//如果没有修复成功判断是否24小时内修复过，如果存在则是修复失败
                    $tasks_data['repairStatus'] = 4;
                    $tasks = [
                        'type' => 'Module_Task_Repair',
                        'method' => 'ydResult',
                        'data' =>$tasks_data
                    ];
                    Func_Log::write('repair',   'yidong','task '. json_encode($tasks));
                    @Module_Queue_Base::addQueue($tasks,0,2);
                }
            }
            //添加更新批次案件状态队列
            $tasks = [
                'type' => 'Module_Task_Repair',
                'method' => 'updateBatch',
                'data' =>['batchId'=>$batch['id'],'repairType'=>$batch['repairType'],'batchNo'=>$batch['batchNo']]
            ];
            Func_Log::write('repair',   'yidong','task '. json_encode($tasks));
            @Module_Queue_Base::addQueue($tasks,0,2);
        }elseif($batch['repairType'] == 2){ //联通查看修复状态
            $repair_result = Lib_Repair_Implement_LianTong::getInstance()->getRepairResult(['batchNo'=>$batch['batchNo']]);
            if($repair_result['code'] != '000' ){//请求返回异常
                Func_Log::write('repair','liantong','get repair result -- error response '.'responsedata : '. json_encode($repair_result));
                break;
            }
            if(in_array($repair_result['data']['repairBatchStatus'],['RPBS001','RPBS002'])){//待修复或修复中
                Func_Log::write('repair','liantong','error: get repair result --Under repair  response '.'responsedata : '. json_encode($repair_result));
                break;
            }
            $repairData = $repair_result['data']['repairDatas'];
            $uniqe = [];
            foreach($repairData as $key => $val){
                //凑数数据剔除
                if('63640264849A87C90356129D99EA165E37AA5FABC1FEA46906DF1A7CA50DB492' == $val['idCard']) {
                    continue;
                }
                $val['batchId'] = $batch['id'];
                $val['batchNo'] = $batch['batchNo'];
                $tasks = [
                    'type' => 'Module_Task_Repair',
                    'method' => 'ltResult',
                    'data' =>$val
                ];
                Func_Log::write('repair',   'liantong','task '. json_encode($tasks));
                @Module_Queue_Base::addQueue($tasks,0,2);
            }
            //添加更新批次案件状态队列
            $tasks = [
                'type' => 'Module_Task_Repair',
                'method' => 'updateBatch',
                'data' =>['batchId'=>$batch['id'],'repairType'=>$batch['repairType'],'batchNo'=>$batch['batchNo']]
            ];
            Func_Log::write('repair',   'liantong','task '. json_encode($tasks));
            @Module_Queue_Base::addQueue($tasks,0,2);
        }
    }



}